package com.lau.config;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.core.MethodParameter;
import org.springframework.web.bind.ServletRequestUtils;
import org.springframework.web.bind.support.WebDataBinderFactory;
import org.springframework.web.context.request.NativeWebRequest;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.method.support.ModelAndViewContainer;

import javax.servlet.http.HttpServletRequest;

/**
 * @author:Lau
 * @create: 2023-11-28 16:18
 * @Description: 自定义方法参数解析器
 */
public class MethodArgumentResolver implements HandlerMethodArgumentResolver {
    @Override
    public boolean supportsParameter(MethodParameter methodParameter) {
        //方法参数中是否包含 Page, true 进入resolveArgument()
        return methodParameter.getParameterType().isAssignableFrom(Page.class);
    }

    @Override
    public Object resolveArgument(MethodParameter methodParameter, ModelAndViewContainer modelAndViewContainer, NativeWebRequest webRequest, WebDataBinderFactory webDataBinderFactory) throws Exception {
        HttpServletRequest request = webRequest.getNativeRequest(HttpServletRequest.class);
        int current = ServletRequestUtils.getIntParameter(request, "cuurent", 1);
        int size = ServletRequestUtils.getIntParameter(request, "size", 11);
        return new Page<>(current, size);
    }
}
